<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item,index) in list"
        :key="index"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | relative }}</span>
            <van-button size="mini" type="default" @click="clickShowReply(item)">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="content"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup v-model="isShowReply" round position="bottom" :style="{ height: '85%' }">
      <CommentReply v-if="isShowReply" :current_comment="comment" :art_id="art_id" @close="closeFn" />
    </van-popup>
  </div>
</template>

<script>
import { reqGetArtComments, reqAddComment } from '@/api/comment'
import CommentReply from './commentReply.vue'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {
    // eslint-disable-next-line vue/prop-name-casing
    art_id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      isShowReply: false,
      comment: {}
    }
  },
  created() {
    console.log('评论组件创建了')
  },
  methods: {
    async  onLoad() {
      // 异步更新数据
      const res = await reqGetArtComments(this.art_id, this.offset)
      const arr = res.data.results
      this.list = [...this.list, ...arr]

      // 加载状态结束
      this.loading = false

      // 数据全部加载完成
      if (res.data.results.length === 0) {
        this.finished = true
      }
      this.offset = res.data.last_id
      console.log(this.list)
    },

    async addComment() {
      if (this.content.trim === '') return
      const res = await reqAddComment(this.art_id, this.content)
      this.list.unshift(res.data.new_obj)
      console.log(res)
      this.$toast.success('发布成功')
      this.content = ''
    },

    clickShowReply(item) {
      this.isShowReply = true
      this.comment = item
    },

    closeFn() {
      this.isShowReply = false
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
